<?php
/**
 * (commands) Amazon订单（补单）
 */
namespace App\Console\Commands\SpApi\Order;

use App\Logic\Amazon\Api\OrderWork;
use App\Models\Basics\AmazonAccount;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class GetOrdersByAccountId extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sp-order:get-orders-account {accountId=0}';

    /**
     * The console command description.
     * @var string
     */
    protected $description = 'exe func';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $accountId = $this->argument('accountId');
        $where = $accountId ? ['id' => $accountId] : ['api' => 1];
        if ($accountId) {
            //单api账号（指定账号）
            $accounts = AmazonAccount::optionsForApi($where);
            if (!$accounts) {
                $this->error('accounts error!');
            }
            $accounts[0]->api = 0; //停用
            $accounts[0]->save();
        }else{
            //全api账号
            $accounts = AmazonAccount::optionsForApi($where);
        }

        OrderWork::getOrders($accounts);

        if ($accountId) {
            //单api账号（指定账号）
            $accounts[0]->api = 1; //启用
            $accounts[0]->save();
        }

        Log::info('order-id:'.date('Y-m-d H:i:s',time()));
        $this->info('finish!');
    }
}
